#include <cstdint>
#include <iostream>
#include <ostream>
#include <string>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "logMessage.hpp"

using std::cout;
using std::cin;
using std::endl;
using std::getline;
using std::string;

static void Usage(const string porc)
{
    cout<<"Usage::\n\t"<<porc<<"server_IP server_Port"<<endl;
}

int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        exit(1);
    }
    string server_IP=argv[1];
    uint16_t server_Port=atoi(argv[2]);;

    int sockFd=socket(AF_INET,SOCK_DGRAM,0);
    if(sockFd<0)
    {
        logMessage(FATAL,"socket() failed:: %s :%d",strerror(errno),sockFd);
        exit(2);
    }
    logMessage(DEBUG,"socket() success:: %d",sockFd);

    struct sockaddr_in server;
    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(server_Port);
    server.sin_addr.s_addr=inet_addr(server_IP.c_str());

    string inBuffer;
    while(true)
    {
        cout<<"Please Enter >>";
        getline(cin,inBuffer);
        sendto(sockFd,inBuffer.c_str(),inBuffer.size(),0,(const struct sockaddr*)&server,sizeof(server));
    }

    close(sockFd);
    return 0;
}
